﻿#include "StdAfx.h"
#include "DBCEx.h"
#include "Stmt.h"
#include "Lite_Util.h"
#include "Globalvf.h"
#include "Lite_Util.h"
#include "StmtEx.h"
#include "sqlite3.h"
//▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
// DBC 생성자/파괴자. 생성/삭제.
/**
	@brief 기본 생성자.
*/
CDBCEx::CDBCEx(CENVEx * pEnv)
{
	m_nMagic = DBC_MAGIC;
    m_nNatErr = 0;			/**< Native error code */
    memset(m_szSQLState,0,6);		/**< SQL state for SQLError() */
    memset(m_szLogMsg,0,1024);	/**< Message for SQLError() */
    memset(m_szDsn,0,50);			/**< ODBC data source name */
    memset(m_szDbName,0,50);		/**< SQLITE database name */
	m_pEnv = pEnv;
	m_pEngDbc = NULL;
	nowchar = 0;
	dobigint = 0;
	for( int i = 0 ; i < STMT_COUNT ;i++)
	{
		m_pStmt[i] = NULL;
	}
	oemcp = 1;

	t0  = 0;
	busyint = 0;
	ov3 = NULL;
	ov3val =0;
    m_nAutoCommit = 1;		/**< Auto commit state */
    m_nIntrans = 0;		/**< True when transaction started */
	nowchar = 0;
	dobigint = 0;
	shortnames =  0;
	longnames =  0;
	nocreat =  0;
	nocreat =  0;
	fksupport =  0;
	curtype =  3;
	step_enable = 0;
	trans_disable = 0;
	s3stmt_needmeta = 0;
	m_pszSQLiteSQL = NULL;
}

/**
	@brief 기본 파괴자.
*/
CDBCEx::~CDBCEx()
{
	for( int i = 0 ; i < STMT_COUNT ;i++)
	{
		if ( NULL != m_pStmt[i])
			delete m_pStmt[i];
	}
	if( NULL != m_pEngDbc)
	{
		lite_CloseDBC(m_pEngDbc);      ///< DBC 해제 함수.
	}
}

/**
	@brief CSTMTEx 객체를 만든다.
*/
CSTMTEx * CDBCEx::CreateSTMT()
{
	for( int i = 0 ; i < STMT_COUNT ;i++)
	{
		if( NULL == m_pStmt[i])
		{
			m_pStmt[i] = new CSTMTEx(i,(CDBCEx*)this);
			return m_pStmt[i];
		}
		else if( m_pStmt[i]->m_bUse == FALSE)
		{
			m_pStmt[i]->m_bUse = TRUE;
			return m_pStmt[i]; 
		}
	}
	return NULL;
}


/**
	@brief Lite_Stmt 객체를 만든다.
*/
Lite_Stmt * CDBCEx::CreateLiteSTMT()
{
	return lite_CreateSTMT(m_pEngDbc);
}

/**
	@brief sqlite3_free가 필요한 변수인 m_pszSQLiteSQL 메모리 해제.
*/
void CDBCEx::FreeSQLiteSQL()
{
	if( NULL != m_pszSQLiteSQL)
		sqlite3_free(m_pszSQLiteSQL);
	m_pszSQLiteSQL = NULL;
}

void CDBCEx::SetStatus(int nErr, char * pMsg, char * pState)
{
}

BOOL CDBCEx::Connect(char *pIP, int nPort, int nTimeout, int nRetry, char *pUID, char * pPWD)
{
	if( NULL == m_pEngDbc)
		m_pEngDbc = lite_CreateDBC();
	return lite_ConnectDB(m_pEngDbc,pIP, nPort, nTimeout,3, pUID, pPWD); // 서버와 연결 시도
}

BOOL CDBCEx::IsConnected()
{
	if( NULL != m_pEngDbc)
		return lite_IsConnected(m_pEngDbc);
	return FALSE;
}

void CDBCEx::Disconnect()
{
	if( NULL != m_pEngDbc)
		return lite_DisconnectDB(m_pEngDbc);
}

const char * CDBCEx::GetErr()
{
	return NULL;
}

BOOL CDBCEx::Exec(const char * pszSQL)   
{
	return lite_Exec(m_pEngDbc, (char*)pszSQL);
}

BOOL CDBCEx::Begin(char** pErr)
{
	return TRUE;
}
BOOL CDBCEx::Commit(char** pErr)
{
	return TRUE;
}
BOOL CDBCEx::RollBack(char** pErr)
{
	return TRUE;
}

void CDBCEx::SetState(int naterr, char *msg, char *st, ...)
{
	va_list ap;
    naterr = naterr;
    m_szLogMsg[0] = '\0';
    if (msg) 
	{
		int count;
		va_start(ap, st);
		count = vsnprintf((char *) m_szLogMsg, sizeof (m_szLogMsg), msg, ap);
		va_end(ap);
		if (count < 0) 
		{
			m_szLogMsg[sizeof (m_szLogMsg) - 1] = '\0';
		}
    }
    if (!st) 
	{
		st = "?????";
    }
    strncpy_s(m_szSQLState,6, st, 5);
    m_szSQLState[5] = '\0';
}


